﻿#include "FITKAcousticsVTKGraphObjectSEASelectMergedData.h"
#include "FITKAcousticsVTKGraphActor.h"

// VTK
#include <vtkProperty.h>
#include <vtkPoints.h>
#include <vtkMultiBlockDataSet.h>
#include <vtkCompositeDataGeometryFilter.h>

// Graph and filter
#include "FITKAcousticsVTKCommons.h"
#include "FITKAcousticsVTKSharedDataManager.h"
#include "FITK_Interface/FITKVTKAlgorithm/FITKGraphActor.h"

namespace Exchange
{
    FITKAcousticsVTKGraphObjectSEASelectMergedData::FITKAcousticsVTKGraphObjectSEASelectMergedData()
        : FITKAcousticsVTKGraphObject3D()
    {
        // Initialize.
        init();

        // Set the layer need to be rendered.
        setRenderLayer(2);
    }

    FITKAcousticsVTKGraphObjectSEASelectMergedData::~FITKAcousticsVTKGraphObjectSEASelectMergedData()
    {
        // Delete vtk objects.
        deleteVtkObj(m_mDataSet);
        deleteVtkObj(m_comp2Poly);

        m_selectedDataIds.clear();
    }

    void FITKAcousticsVTKGraphObjectSEASelectMergedData::init()
    {
        // Create the multi block dataset.
        m_mDataSet = vtkMultiBlockDataSet::New();

        // Create the filter.
        m_comp2Poly = vtkCompositeDataGeometryFilter::New();
        m_comp2Poly->SetInputData(m_mDataSet);

        // Create the actor.
        //@{
        m_fActor = FITKAcousticsVTKGraphActor::New();
        m_fActor->setGraphObject(this);
        m_fActor->SetPickable(false);
        m_fActor->GetProperty()->SetRepresentation(1);

        // Default color: red.
        m_fActor->GetProperty()->SetColor(1., 0., 0.);

        // Point size and line width.
        m_fActor->GetProperty()->SetPointSize(FITKAcousticsVTKCommons::s_highlightPointSize);
        m_fActor->GetProperty()->SetLineWidth(FITKAcousticsVTKCommons::s_highlightLineWidth);

        // Transparent.
        // m_fActor->GetProperty()->SetOpacity(1 - FITKAcousticsVTKCommons::s_defaultTransparency);

        m_fActor->setInputConnection(m_comp2Poly->GetOutputPort());
        addActor(m_fActor);
        //@}
    }

    void FITKAcousticsVTKGraphObjectSEASelectMergedData::setSelectData(QVector<int> & ids)
    {
        // Clear the data first.
        clearData();

        if (!m_mDataSet)
        {
            return;
        }

        for (const int & id : ids)
        {
            // Check the id.
            if (m_selectedDataIds.contains(id))
            {
                return;
            }

            // Get the dataset.
            vtkDataSet* dataSet = FITKAcousticsVTKSharedDataManager::GetInstance()->getDataSetByDataId(id);
            if (!dataSet)
            {
                return;
            }

            // Add the block.
            int nBlocks = m_mDataSet->GetNumberOfBlocks();
            m_mDataSet->SetBlock(nBlocks, dataSet);
        }
    }

    void FITKAcousticsVTKGraphObjectSEASelectMergedData::addSelectData(int id)
    {
        // Check the id.
        if (m_selectedDataIds.contains(id) || !m_mDataSet)
        {
            return;
        }

        // Get the dataset.
        vtkDataSet* dataSet = FITKAcousticsVTKSharedDataManager::GetInstance()->getDataSetByDataId(id);
        if (!dataSet)
        {
            return;
        }

        // Add the block.
        int nBlocks = m_mDataSet->GetNumberOfBlocks();
        m_mDataSet->SetBlock(nBlocks, dataSet);        

        // Save the ids.
        m_selectedDataIds.push_back(id);
    }

    void FITKAcousticsVTKGraphObjectSEASelectMergedData::setColor(QColor color)
    {
        if (!m_fActor)
        {
            return;
        }

        m_fActor->setColor(color);
    }

    void FITKAcousticsVTKGraphObjectSEASelectMergedData::clearData()
    {
        if (!m_mDataSet)
        {
            return;
        }

        // Remove all blocks.
        int nBlocks = m_mDataSet->GetNumberOfBlocks();
        for (int i = nBlocks - 1; i >= 0; i--)
        {
            m_mDataSet->RemoveBlock(i);
        }

        // Clear the id list.
        m_selectedDataIds.clear();
    }
}   // namespace Exchange